
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;

bool fileExist(string fileName){
	ifstream fin(fileName);
	if(fin.good()){
		fin.close();
		return true;
	}
	else {
		return false;
	}
	

}

ifstream createInputFStream(string fileName){
	if(!fileExist(fileName)){
		throw fileName + " not exist";
	}
	ifstream fin(fileName);
	return fin;
}

ofstream createOutputFStream(string fileName){
	ofstream fout;
	if(fileExist(fileName)){
		cout << fileName + " already exist, clear the file? y/n: ";
		string s;
		cin >> s;
		if(s != "y" && s != "Y"){
			exit(1219);
		}
	}
	fout.open(fileName);
	return fout;
}

int randomInt(int minValue, int maxValue){
	int res = rand() % (maxValue - minValue + 1) + minValue;
	return res;
}

double randomDouble(double minValue, double maxValue){
	double res = minValue + static_cast<double>(rand()) / (static_cast<double>(RAND_MAX / (maxValue - minValue))); 
	return res;
}


string randomString(int len, string chars){
	char res[len + 1];
	res[len] = '\0';
	for (int i = 0; i < len; i++) {
			res[i] = chars[randomInt(0, chars.size() - 1)];
	}
	return res;
}


// density是图的密度，值0到1
// 随机生成一个图，无重边无自环，可以当作无向图来用，也可以当作有向无环图
vector<pair<int, int>> randomGraph(int n, double density){
	vector<pair<int, int>> res;
	for(int i = 1; i < n; i++){
		for(int j = i + 1; j <= n; j++){
			if(randomDouble(0, 1) < density){
				res.push_back({i, j});
			}
		}
	}
	return res;
}

// 随机生成一个图，有重边和自环
vector<pair<int, int>> randomGraphRep(int n, double density){
	vector<pair<int, int>> res;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(randomDouble(0, 1) < density){
				res.push_back({i, j});
			}
		}
	}
	return res;
}